package cn.iocoder.yudao.module.ai.graph.node.customerservice;

import com.alibaba.cloud.ai.graph.OverAllState;
import com.alibaba.cloud.ai.graph.action.NodeAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 转人工客服节点 - 将复杂问题转接给人工客服
 * 
 * @author yudao
 * @since 2025/10/17
 */
public class TransferToHumanNode implements NodeAction {

    private static final Logger logger = LoggerFactory.getLogger(TransferToHumanNode.class);

    @Override
    public Map<String, Object> apply(OverAllState state) {
        logger.info("=== TransferToHumanNode 开始执行 ===");
        
        String customerId = state.value("customer_id", "");
        String query = state.value("customer_query", "");
        String category = state.value("query_category", "");
        String priority = state.value("query_priority", "");
        String humanComments = state.value("human_comments", "");
        
        logger.info("🔄 转接人工客服");
        logger.info("客户 ID：{}", customerId);
        logger.info("问题类别：{}", category);
        logger.info("优先级：{}", priority);
        
        if (!humanComments.isEmpty()) {
            logger.info("转接备注：{}", humanComments);
        }
        
        // 模拟转接逻辑
        String assignedAgent = assignToAgent(priority, category);
        
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("transfer_status", "success");
        resultMap.put("assigned_agent", assignedAgent);
        resultMap.put("transfer_reason", "需要人工专业处理");
        
        logger.info("✓ 已转接给客服：{}", assignedAgent);
        
        // 这里可以发送通知给客服系统
        notifyAgentSystem(customerId, query, assignedAgent);
        
        return resultMap;
    }

    /**
     * 根据优先级和类别分配客服
     */
    private String assignToAgent(String priority, String category) {
        if ("high".equals(priority)) {
            return "高级客服-张三";
        } else if ("投诉建议".equals(category)) {
            return "投诉处理专员-李四";
        } else {
            return "客服专员-王五";
        }
    }

    /**
     * 通知客服系统
     */
    private void notifyAgentSystem(String customerId, String query, String agent) {
        logger.info("📢 [通知客服系统] 客户：{}, 分配给：{}", customerId, agent);
        // 实际项目中，这里应该调用客服系统 API
    }
}

